package top.songxs.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import top.songxs.base.BaseDao;
import top.songxs.base.BaseServiceImpl;
import top.songxs.dao.UserFollowDao;
import top.songxs.entity.UserFollow;
import top.songxs.result.Result;
import top.songxs.service.DictService;
import top.songxs.service.UserFollowService;
import top.songxs.vo.UserFollowVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service(interfaceClass = UserFollowService.class)
@Transactional
public class UserFollowServiceImpl extends BaseServiceImpl<UserFollow> implements UserFollowService {
    @Autowired
    private UserFollowDao userFollowDao;
    //@Autowired
    @Reference
    private DictService dictService;

    @Override
    protected BaseDao<UserFollow> getEntityDao() {
        return userFollowDao;
    }

    @Override
    public boolean isFollow(Long houseId, Long userId) {
        int count = this.userFollowDao.isFollow(houseId,userId);
        if(count>0){
            return true;
        }
        return false;
    }

    @Override
    public PageInfo<UserFollowVo> findListPage(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum,pageSize);

        Page<UserFollowVo> page =  this.userFollowDao.findListPage(userId);

        List<UserFollowVo> userFollowVoList = page.getResult();
        for(UserFollowVo userFollowVo:userFollowVoList){
            String houseTypeName = this.dictService.getNameById(userFollowVo.getHouseTypeId());
            userFollowVo.setHouseTypeName(houseTypeName);
            String floorName = this.dictService.getNameById(userFollowVo.getFloorId());
            userFollowVo.setFloorName(floorName);
            String directionName = this.dictService.getNameById(userFollowVo.getDirectionId());
            userFollowVo.setDirectionName(directionName);
        }

        return new PageInfo<>(page.getResult());
    }

    @Override
    public void cancelFollow(Long id) {
        this.userFollowDao.cancelFollow(id);
    }
}
